//
//  SheetTabelview.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/28.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SheetTabelview: UIView ,UITableViewDelegate, UITableViewDataSource{
    var config:ComonConfig?
    var model:CommonSheetModel?
    var dataSource:SelectmangerDelegate?
    var selectIndex:Int  = 0
    init(config:SelectConfig ,model:SheetModel ,dataSource:SelectmangerDelegate) {
        super.init(frame: CGRect.zero)
        self.config = config as? ComonConfig
        self.model = model as? CommonSheetModel
        if self.model?.curretnStr != nil {
            if self.model!.titleArr.index(of: self.model!.curretnStr!) != nil {
                selectIndex = self.model!.titleArr.index(of: self.model!.curretnStr!)!
            }
        }
        self.dataSource = dataSource
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        let tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = (config?.canScro)!
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.themeBackground = ThemeManager.ThemeColorKeys.background
        self.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.left.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    //MARK: tableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (model?.titleArr.count)!
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellName = "identify"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellName)
        if cell == nil {
           cell = UITableViewCell(style: .default, reuseIdentifier: cellName)
            cell?.contentView.themeBackground = ThemeManager.ThemeColorKeys.background
            cell?.selectedBackgroundView = UIView(frame: self.bounds)
            cell?.selectedBackgroundView?.themeBackground = ThemeManager.ThemeColorKeys.selectTabColor
        }
        cell?.contentView.backgroundColor = UIColor.clear
        cell?.contentView.themeBackground = ThemeManager.ThemeColorKeys.background
        cell?.textLabel?.text = model?.titleArr[indexPath.row]
        cell?.textLabel?.font = UIFont.systemFont(ofSize: CGFloat((config?.titleFont)!))
        cell?.textLabel?.textColor = indexPath.row==selectIndex ? config?.selectorColor :  config?.cancelColor
        cell?.textLabel?.textAlignment = NSTextAlignment.center
        
        let lineView = UIView()
        lineView.themeBackground = ThemeManager.ThemeColorKeys.tabLineColor
        cell?.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }


        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return (config?.cellHeight)!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         model?.selectStr = model?.titleArr[indexPath.row]
         model?.selectIndex = indexPath.row
        if dataSource != nil {
            self.dataSource?.selectModel(model: model!)
        }
        SelectActionManger.manager.close()
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
